package com.youlei.newgraduateproject.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youlei.newgraduateproject.dynamic.datasource.config.DynamicContextHolder;
import com.youlei.newgraduateproject.entity.CommonResult;
import com.youlei.newgraduateproject.entity.Employees;
import com.youlei.newgraduateproject.entity.Employees2;
import com.youlei.newgraduateproject.service.EmployeesService;
import com.youlei.newgraduateproject.service.SysUserService;
import com.youlei.newgraduateproject.service.impl.EmployeesServiceImpl2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author youlei
 * @version 1.0
 * @Date 2021-05-08-12:51
 */
@RestController
public class TestController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    EmployeesService employeesService;

    @Autowired
    EmployeesServiceImpl2 employeesServiceImpl2;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping(value = "/test")
    public CommonResult testController(){
        //int s = 10/0;
        return new CommonResult<>(200,"测试成功",sysUserService.list()) ;
    }

    @GetMapping("/test/pass")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    public CommonResult passEncode() {   // 密码加密
          String pass = bCryptPasswordEncoder.encode("111111");      // 密码验证
          boolean matches = bCryptPasswordEncoder.matches("111111", pass);
          return new CommonResult(200,"登录成功",
                  MapUtil
                          .builder()
                          .put("pass", pass)
                          .put("marches", matches)
                          .build()   );}




    @GetMapping("/testdynamicdatasource/{id}")
    public Employees2 testDynamicDatasource(@PathVariable("id") Integer id){

        //List<Employees> list = employeesService.list(new QueryWrapper<Employees>().select("employee_name"));
        Employees2 employees2 = employeesServiceImpl2.getById(id);
        return employees2;

    }
    @GetMapping("/testdynamicdatasource")
    public List<Employees> testDynamicDatasource(){

        List<Employees> list = employeesService.list();
        System.out.println(DynamicContextHolder.peek());
        return list;

    }
    @GetMapping("/testdynamicdatasource2/{id}")
    public Employees2 testDynamicDatasource2(@PathVariable("id") Integer id){

        Employees2 employee = employeesService.getByEmployeeId(id);
        return employee;

    }
}
